package xkp.lesson2;

import java.io.*;

public class TestDisk {
    public static void main(String[] args) throws IOException {
        copyFileByBuf("E:\\visio2013_53466.zip","d:");
    }

    public static void copyFileByBuf(String srcPath,String destDirPath) throws IOException {
        //源文件
        File srcFile = new File(srcPath);
        //目标文件夹
        File destDirFile = new File(destDirPath);
        if(!destDirFile.exists()){
            destDirFile.mkdirs();
        }
        //目标文件
        File descFile = new File(destDirPath+File.separator+srcFile.getName());
        //如果此处有检查性异常可以使用Alt+Enter快速完成代码
        //字节输入流
        FileInputStream inputStream = new FileInputStream(srcFile);
        //对输入流包装
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        //字节输出流
        FileOutputStream outputStream = new FileOutputStream(descFile);
        //对输出流进行包装
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

        //开辟内存，存储从流中读取到的数据
        byte[] buf = new byte[1024*16];
        //开始读取数据 length为实际读取到的字节个数，如果读到文件结尾，则返回-1
        int length = bufferedInputStream.read(buf);
        while(length != -1){
            //向目标文件中写数据
            bufferedOutputStream.write(buf,0,length);
            length = bufferedInputStream.read(buf);
        }
        //释放资源
        bufferedInputStream.close();
        //输出流关闭前切记要流中的缓存数据给推送出去
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
    }
}
